#!/bin/bash
################################################################################
# Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The OpenAirInterface Software Alliance licenses this file to You under 
# the Apache License, Version 2.0  (the "License"); you may not use this file
# except in compliance with the License.  
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
# For more information about the OpenAirInterface (OAI) Software Alliance:
#      contact@openairinterface.org
################################################################################

# file run_cassandra
# brief run script for Cassandra.
# author  Tien-Thinh NGUYEN
# company Eurecom
# email:  tien-thinh.nguyen@eurecom.fr 


################################
# include helper functions
################################
THIS_SCRIPT_PATH=$(dirname $(readlink -f $0))
source $THIS_SCRIPT_PATH/../build/tools/build_helper

declare    g_cassandra_default_config_file="/usr/local/etc/oai/cassandra.conf"

function help()
{
  set_openair_env
  echo_error " "
  echo_error "Usage: run_cassandra [OPTION]..."
  echo_error "Run Cassandra DB."
  echo_error " "
  echo_error "Options:"
  echo_error "Mandatory arguments to long options are mandatory for short options too."
  echo_error "  -c, --config-file  filename     Config file to be used by Cassandra if you don't want to use the default one: $g_cassandra_default_config_file"
  echo_error "  -h, --help                      Print this help."
  echo_error "  -k, --kill                      Kill the running local Cassandra."
}

function main() 
{
  local -i run_gdb=0
  local -i var_check_install_cassandra_files=0
  local    cassandra_config_file=$g_cassandra_default_config_file
  local    exe_arguments=" "

  until [ -z "$1" ]
    do
    case "$1" in
      -c | --config-file)
        cassandra_config_file=$2
        echo "setting config file to: $cassandra_config_file"
        shift 2;
        ;;
      -h | --help)
        help
        exit 0
        ;;
      -k | --kill)
        $SUDO killall -q cassandra        
        shift 1;
        exit 0
        ;;
      *)   
        echo "Unknown option $1"
        help
        exit 0
        ;;
    esac
  done

  set_openair_env 
  cecho "OPENAIRCN_DIR   = $OPENAIRCN_DIR" $green

  #####################################
  # Install config files
  #####################################
  if [ ! -f $cassandra_config_file ]; then 
    if [ $g_cassandra_default_config_file != $cassandra_config_file ]; then
      echo_fatal "Please provide -c|--config-file valid argument (\"$cassandra_config_file\" not a valid file)"
    fi
  fi
  
 
  
  #TODO: Verify that Cassandra is running with the updated configuration (nodetool) 
  
  #get Cassandra_Server_IP from the configuration file
  value="`cat $cassandra_config_file | cut -d "#" -f1 | grep 'cassandra_server_IP' | tr -d " " | grep "="`"
  eval $value
  var_name="cassandra_server_IP"
  Cassandra_Server_IP=${!var_name}

  #Create the HSS Database Schema
  cqlsh --file $OPENAIRCN_DIR/src/hss_rel14/db/oai_db.cql $Cassandra_Server_IP
  
  if [ $? -ne 0 ]; then
     echo_error "HSS Database creation failed"
     return 1
  else
     echo_success "HSS Database creation succeeded"
  fi
    
  #Add Test Data to Cassandra
  #./data_provisioning_users.sh <IMSI> <MSISDN> <APN> <Key> <No of UEs> <Cassandra_Server_IP>
  #The following example will add 200 entries:
  cd $THIS_SCRIPT_PATH
  ./data_provisioning_users.sh 208014567891234 1122334455 apn1 465B5CE8B199B49FAA5F0A2EE238A6BC 200 $Cassandra_Server_IP
  
  if [ $? -ne 0 ]; then
     echo_error "Data provisioning for users failed"
     return 1
  else
     echo_success "Data provisioning for users succeeded"
  fi
  
  #Add server entries into the database
  #./data_provisioning_mme.sh <ID> <ISDN> <HOST> <REALM> <UE  Reachability> <Cassandra_Server_IP>
  ./data_provisioning_mme.sh 1 19136246000 mme.localdomain localdomain 1 $Cassandra_Server_IP
      
  if [ $? -ne 0 ]; then
     echo_error "Data provisioning for mme failed"
     return 1
  else
     echo_success "Data provisioning for mme succeeded"
  fi
  
  #start Cassandra (if necessary)
  $SUDO service cassandra start
  return $?
}


main "$@"
